package com.engine.modules.vector.mapper;

import com.engine.common.exception.SystemTableException;
import com.engine.init.Globle;
import com.engine.init.entity.TablePropertieEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author FY
 * @version 1.0.0
 * @ClassName VectorToolMapper.java
 * @Description TODO
 * @createTime 2022年01月05日
 */
@Repository
@Slf4j
public class VectorToolMapper {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public int caculagteShapeArea(String id, String tableName) {
        TablePropertieEntity tablePropertieEntity = Globle.TablePropertiesMap.get(tableName);
        if (tablePropertieEntity == null)
            throw new NullPointerException("数据库中表不存在或未添加进数据目录");
        if (!tablePropertieEntity.getGeneralColumns().contains("shape_area"))
            throw new SystemTableException("表中缺少shape_area字段");
        String sql = "UPDATE " + tableName + " AS t1 SET shape_area=round(\"numeric\"" +
                "(st_area(ST_Transform(geom,cast(ST_X(ST_Transform(st_centroid(geom),4326))/3 AS int4)+4488))),2) ";
        return jdbcTemplate.update(sql, id);
    }

    public int caculagteSysMJM(String id, String tableName) {
        TablePropertieEntity tablePropertieEntity = Globle.TablePropertiesMap.get(tableName);
        if (tablePropertieEntity == null)
            throw new NullPointerException("数据库中表不存在或未添加进数据目录");
        if (!tablePropertieEntity.getGeneralColumns().contains("shape_area"))
            throw new SystemTableException("表中缺少shape_area字段");
        if (!tablePropertieEntity.getGeneralColumns().contains("sysmjm"))
            throw new SystemTableException("表中缺少sysmjm字段");
        String sql = "UPDATE " + tableName + " AS t1 SET shape_area=round(\"numeric\"" +
                "(st_area(ST_Transform(geom,cast(ST_X(ST_Transform(st_centroid(geom),4326))/3 AS int4)+4488))),2) ";
        jdbcTemplate.update(sql, id);

        sql = "UPDATE " + tableName + " SET sysmjm=round(\"numeric\"(shape_area/666.0),2)";
        return jdbcTemplate.update(sql, id);
    }
}
